package com.neu.service;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.common.JwtTokenUtils;
import com.neu.dao.AdminDao;
import com.neu.dao.HireDao;
import com.neu.dao.HotelDao;
import com.neu.dao.ReserveDao;
import com.neu.entity.*;
import com.neu.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class HireService {
    @Autowired
    private HireDao hireDao;

    @Autowired
    private AdminDao adminDao;
//    @Autowired
//    private HotelDao hotelDao;
//    @Autowired
//    private AdminDao adminDao;

    public PageInfo<Hire> findBySearch(Params params) {
        Admin user = JwtTokenUtils.getCurrentUser();
        if(ObjectUtil.isNull(user)){
            throw new CustomException("从token中未解析到用户信息，请重新登录");
        }
        if("ROLE_STUDENT".equals(user.getRole())){
            params.setStudentId(user.getId());
        }
        if("ROLE_TUTOR".equals(user.getRole())){
            params.setTutorId(user.getId());
        }
        //开启分页查询
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        //接下来的查询会自动按照当前开启的分页设置来查询
        //List<Hire> list = hireDao.findBySearch(params);
        List<Hire> list = hireDao.findBySearch(params);
//        if(CollectionUtil.isEmpty(list)){
//            return PageInfo.of(new ArrayList<>());
//        }
//        for(Hire hire:list){
//            if(ObjectUtil.isNotEmpty(hire.getTutorId())){
//                Admin admin = adminDao.selectByPrimaryKey(hire.getTutorId());
//                if(ObjectUtil.isNotEmpty(admin)){
//                    hire.setTutorName(admin.getName());
//                    hire.setPrice(admin.getPrice());
//                }
//            }
//            if(ObjectUtil.isNotEmpty(hire.getUserId())){
//                Admin admin = adminDao.selectByPrimaryKey(hire.getUserId());
//                if(ObjectUtil.isNotEmpty(admin)){
//                    hire.setUserName(admin.getName());
//                }
//
//            }
//        }
        return PageInfo.of(list);
    }

    public void add(Hire hire) {
        hireDao.insertSelective(hire);
    }

    public void update(Hire hire) {
        hireDao.updateByPrimaryKeySelective(hire);
    }

    public void delete(Integer id) {
        hireDao.deleteByPrimaryKey(id);
    }

    public void audit(Hire hire) {
        hireDao.updateByPrimaryKeySelective(hire);
        
    }

    public void payment(Hire hire) {
        hireDao.updatePay(hire);
    }


    public void interact(Hire hire) {
        hireDao.updateByPrimaryKeySelective(hire);
    }
}
